package com.miaoma.auto.student;

import com.beracash.common.ctrl.CommonCtrl;
import com.beracash.core.constants.ApiCodeEnum;
import com.beracash.core.model.ApiRes;
import com.miaoma.auto.student.Student;
import com.miaoma.auto.student.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/anon/student")
public class StudentController extends CommonCtrl {

    @Autowired
    private StudentService studentService;

    // 获取学生列表（支持分页和条件查询）
    @GetMapping
    public ApiRes<Page<Student>> list(@RequestParam(required = false, defaultValue = "1") int page
                                   , @RequestParam(required = false, defaultValue = "10") int size
                                   , @RequestParam(required = false) String studentNo
                                   , @RequestParam(required = false) String name
                                   , @RequestParam(required = false) String grade
                                   ) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        // 只有当参数不为空且不为空字符串时才添加条件
        if (StringUtils.isNotBlank(studentNo)) {
            queryWrapper.like("student_no", studentNo);
        }
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(grade)) {
            queryWrapper.eq("grade", grade);
        }

        Page<Student> newPage = new Page<>(page, size);
        Page<Student> resultPage = (Page<Student>) studentService.selectPage(newPage, queryWrapper);
        return ApiRes.ok(resultPage);
    }

    // 添加新学生
    @PostMapping
    public ApiRes<Void> add(@RequestBody Student record) {
        // 设置必要的默认值
        if (record.getId() == null) {
            record.setId(0L); // 默认Id
        }
        if (record.getStudentNo() == null) {
            record.setStudentNo(""); // 默认StudentNo
        }
        if (record.getName() == null) {
            record.setName(""); // 默认Name
        }
        if (record.getGender() == null) {
            record.setGender(""); // 默认Gender
        }
        if (record.getClassName() == null) {
            record.setClassName(""); // 默认ClassName
        }
        if (record.getMajor() == null) {
            record.setMajor(""); // 默认Major
        }
        if (record.getGrade() == null) {
            record.setGrade(""); // 默认Grade
        }
        if (record.getEnrollmentDate() == null) {
            record.setEnrollmentDate(LocalDate.now()); // 默认EnrollmentDate
        }
        if (record.getStatus() == null) {
            record.setStatus(""); // 默认Status
        }
        if (record.getCreatedAt() == null) {
            record.setCreatedAt(LocalDateTime.now()); // 默认CreatedAt
        }
        if (record.getUpdatedAt() == null) {
            record.setUpdatedAt(LocalDateTime.now()); // 默认UpdatedAt
        }

        studentService.insert(record);
        return ApiRes.ok();
    }

    // 获取学生详情
    @GetMapping("/{recordId}")
    public ApiRes<Student> detail(@PathVariable Long recordId) {
        Student record = studentService.selectById(recordId);
        if (record == null) {
            return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, "Student not found");
        }
        return ApiRes.ok(record);
    }

    // 更新学生信息
    @PutMapping("/{recordId}")
    public ApiRes<Void> update(@PathVariable Long recordId, @RequestBody Student record) {
        record.setId(recordId);
        studentService.updateById(record);
        return ApiRes.ok();
    }

    // 删除学生
    @DeleteMapping("/{recordId}")
    public ApiRes<Void> delete(@PathVariable Long recordId) {
        studentService.deleteById(recordId);
        return ApiRes.ok();
    }

    // 批量删除学生
    @DeleteMapping
    public ApiRes<Void> deleteBatch(@RequestBody List<Long> ids) {
        studentService.deleteBatchIds(ids);
        return ApiRes.ok();
    }

    // 根据条件查询学生
    @PostMapping("/search")
    public ApiRes<List<Student>> search(@RequestBody Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        List<Student> list = studentService.selectList(queryWrapper);
        return ApiRes.ok(list);
    }

    // 获取学生数量
    @GetMapping("/count")
    public ApiRes<Long> count(@RequestParam Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        Long count = studentService.selectCount(queryWrapper);
        return ApiRes.ok(count);
    }

    // 获取学生的 Map 数据
    @GetMapping("/maps")
    public ApiRes<List<Map<String, Object>>> listMaps(@RequestParam Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        List<Map<String, Object>> maps = studentService.selectMaps(queryWrapper);
        return ApiRes.ok(maps);
    }

    // 分页查询学生的 Map 数据
    @GetMapping("/mapsPage")
    public ApiRes<Page<Map<String, Object>>> listMapsPage(@RequestParam int page,
                                                          @RequestParam int size,
                                                          @RequestParam Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        Page<Map<String, Object>> mapPage = new Page<>(page, size);
        Page<Map<String, Object>> resultPage = (Page<Map<String, Object>>) studentService.selectMapsPage(mapPage, queryWrapper);
        return ApiRes.ok(resultPage);
    }
}